﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;

namespace Asynchronous {
    /// <summary>
    /// Lógica de interacción para MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        System.ComponentModel.BackgroundWorker b;
        public delegate String mD(String str);
        System.Threading.Thread mTh;
        private delegate void SetBDelegate(Color c);
        public MainWindow() {
            InitializeComponent();
            b = new System.ComponentModel.BackgroundWorker();
            b.DoWork += dw;
            b.ProgressChanged += pgh;
            b.RunWorkerCompleted += complete;
            b.WorkerReportsProgress = true;
            b.WorkerSupportsCancellation = true;
            b.RunWorkerAsync("Param");
            mD miDelegado = new mD(Msj);
            AsyncCallback acm = new AsyncCallback(CallBackMethod);
            miDelegado.BeginInvoke("Jose", acm, miDelegado);
            System.Windows.MessageBox.Show(System.Threading.Thread.CurrentThread.CurrentCulture.Name);
           
        }


        private void dw(Object sender, System.ComponentModel.DoWorkEventArgs args) {

            String tipo = args.Argument.ToString();
          //  System.Windows.MessageBox.Show(tipo);
            for (int i = 0; i < 50; i++) {
                if (b.CancellationPending) {
                    args.Cancel = true;
                    return;
                }
                b.ReportProgress(i + 1);
                for (int j = 0; j < 5000; j++) {
                    System.Console.Write('w');
                }
            }

            args.Result = "listo";
        }

        private void pgh(Object sender, System.ComponentModel.ProgressChangedEventArgs args) {
            pg.Value = args.ProgressPercentage;

        }

        private void complete(Object sender, System.ComponentModel.RunWorkerCompletedEventArgs args) {
            String msj;

            if (args.Cancelled)
                msj = "Proceso Cancelado";
            else
                msj = "Proceso Concluido :" + args.Result;
            System.Windows.MessageBox.Show(msj);
        }

        private void Button_Click(object sender, RoutedEventArgs e) {
            b.CancelAsync();
        }


        private String Msj(String Nombre) {
            System.Windows.MessageBox.Show(Nombre);
            return "Listo";
        }

        private void CallBackMethod(IAsyncResult r) {
            mD miDelegado;
            String result;
            miDelegado = (mD)r.AsyncState;
            result = miDelegado.EndInvoke(r);
            System.Windows.MessageBox.Show(result);
           SetBDelegate mBackD = new SetBDelegate(SetBackGround);
           System.Windows.MessageBox.Show(System.Threading.Thread.CurrentThread.Name);
            this.Dispatcher.BeginInvoke (mBackD, new Object[] { Colors.Red });
        }

        private void SetBackGround(Color c) {
            this.Background = new SolidColorBrush(c);
        }

    }
}
